package com.example.consumptionrecords.controller;

import com.example.consumptionrecords.entity.dto.ConRecordsDTO;
import com.example.consumptionrecords.entity.vo.ConRecordsStatisticsVO;
import com.example.consumptionrecords.entity.http.Response;
import com.example.consumptionrecords.entity.pojo.*;
import com.example.consumptionrecords.entity.vo.HomeCardVO;
import com.example.consumptionrecords.exception.GlobalException;
import com.example.consumptionrecords.service.interfaces.HomeService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/home")
public class HomeController {

    @Resource
    private HomeService homeService;

    @GetMapping("/getHomeCard")
    public ResponseEntity<Response<List<HomeCardVO>>> getHomeCard() {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
        try {
			log.info("用户：{} 查询首页卡片内容", user.getUserPhone());
            List<HomeCardVO> icon = homeService.getCard(user.getId());
			log.info("用户：{} 查询首页卡片内容成功", user.getUserPhone());
            return ResponseEntity.ok(Response.success(icon));
        }  catch (NullPointerException | GlobalException e) {
			log.error("用户：{} 查询首页卡片内容出错", user.getUserPhone(), e);
            return ResponseEntity.ok(Response.error());
        }
    }

    @GetMapping("/selectConRecordsStatistics/{dateType}")
    public ResponseEntity<Response<List<ConRecordsStatisticsVO>>> selectConRecordsStatistics(@PathVariable Integer dateType) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
        try {
			log.info("用户：{} 查询统计数据", user.getUserPhone());
            List<ConRecordsStatisticsVO> conRecordsStatistics = homeService.selectConRecordsStatistics(dateType, user.getId());
			log.info("用户：{} 查询统计数据成功", user.getUserPhone());
            return ResponseEntity.ok(Response.success(conRecordsStatistics));
        }  catch (NullPointerException | GlobalException e) {
			log.error("用户：{} 查询统计数据出错", user.getUserPhone(), e);
            return ResponseEntity.ok(Response.error());
        }
    }

    @GetMapping("/selectConPayChannel")
    public ResponseEntity<Response<List<ConPayChannel>>> selectConPayChannel() {
        try {
			log.info("查询消费渠道");
            List<ConPayChannel> conPayChannels = homeService.selectConPayChannel();
			log.info("查询消费渠道成功");
            return ResponseEntity.ok(Response.success(conPayChannels));
        }  catch (NullPointerException | GlobalException e) {
            log.error("查询消费渠道出错", e);
            return ResponseEntity.ok(Response.error());
        }
    }

    @GetMapping("/selectConPlatform")
    public ResponseEntity<Response<List<ConPlatform>>> selectConPlatform() {
        try {
			log.info("查询支付平台");
            List<ConPlatform> conPlatforms = homeService.selectConPlatform();
			log.info("查询支付平台成功");
            return ResponseEntity.ok(Response.success(conPlatforms));
        }  catch (NullPointerException | GlobalException e) {
            log.error("查询支付平台出错", e);
            return ResponseEntity.ok(Response.error());
        }
    }

    @GetMapping("/selectConType")
    public ResponseEntity<Response<List<ConType>>> selectConType() {
        try {
			log.info("查询消费类型");
            List<ConType> conTypes = homeService.selectConType();
			log.info("查询消费类型成功");
            return ResponseEntity.ok(Response.success(conTypes));
        }  catch (NullPointerException | GlobalException e) {
            log.error("查询消费类型出错", e);
            return ResponseEntity.ok(Response.error());
        }
    }

    @PostMapping("/insertConRecords")
    public ResponseEntity<Response> insertConRecords(@RequestBody ConRecordsDTO conRecordsDTO) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
        try {
			log.info("用户：{} 添加消费记录", user.getUserPhone());
            homeService.insertConRecords(conRecordsDTO, user.getId());
			log.info("用户：{} 添加消费记录成功", user.getUserPhone());
            return ResponseEntity.ok(Response.success());
        }  catch (NullPointerException | GlobalException e) {
			log.error("用户：{} 添加消费记录出错", user.getUserPhone(), e);
            return ResponseEntity.ok(Response.error());
        }
    }
}
